package com.javaclimb.filmbuy.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Consts;
import com.javaclimb.filmbuy.dao.CommentInfoDao;
import com.javaclimb.filmbuy.entity.Account;
import com.javaclimb.filmbuy.entity.CommentInfo;
import com.javaclimb.filmbuy.entity.GoodsInfo;
import com.javaclimb.filmbuy.exception.CustomException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 评价service层
* */
@Service
public class CommentInfoService {

    @Resource
    private CommentInfoDao commentInfoDao;

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    /**
    * 增加一个评价
    * */
    public CommentInfo add(CommentInfo commentInfo){
        commentInfo.setCreateTime(DateUtil.formatDateTime(new Date()));
        String content = commentInfo.getContent();
        if(content.length()>2000){
            commentInfo.setContent(content.substring(0,2000));
        }
        commentInfoDao.insertSelective(commentInfo);
        return commentInfo;
    }

    /**
    * 根据主键ID删除一个评价
    */
    public void delete(Long id){
        commentInfoDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改一个评价
     */
    public void update(CommentInfo commentInfo){
        String content = commentInfo.getContent();
        if(content.length()>2000){
            commentInfo.setContent(content.substring(0,2000));
        }
        commentInfoDao.updateByPrimaryKeySelective(commentInfo);
    }


    /**
    * 根据主键id查询一个评价
    * */
    public CommentInfo findById(Long id){
        return commentInfoDao.selectByPrimaryKey(id);
    }


    /**
     * 查询所有评价
     */
    public List<CommentInfo> findAll(){
        return commentInfoDao.selectAll();
    }

    /**
     *分页查询
     */
    public PageInfo<CommentInfo> findPage(Integer pageNum, Integer pageSize,String content, HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user == null){
            throw new CustomException("1001","session已失效，请重新登录");
        }
        List<CommentInfo> infoList;
        PageHelper.startPage(pageNum,pageSize);
        List<CommentInfo> list = commentInfoDao.findByContent(content,user.getLevel());
        return PageInfo.of(list);
    }

    /**
     * 查询某个电影的所有评价
     */
            public List<CommentInfo> findAll(Long goodsId){
               List<CommentInfo> list = commentInfoDao.findByGoodsId(goodsId);
               if(CollectionUtil.isNotEmpty(list)){
                   for(CommentInfo info : list){
                       Integer level = info.getLevel();
                       if (level ==1 ){
                           info.setUserName(adminInfoService.findById(info.getUserId()).getName());
                       }else{
                           info.setUserName(userInfoService.findById(info.getUserId()).getName());
                       }
                   }
               }
               return list;
            }

}












